package leetcode;

public class ReverseNodesInKGroup {

	 public ListNode reverseKGroup(ListNode head, int k) {
         if(k<=1)return head;
         ListNode pre=head;
         ListNode reverse[]=new ListNode[k];
         int index=0;
         while(head!=null){
        	 reverse[index%k]=head;
        	 if(index%k==k-1){
        		for(int i=0,j=k-1;i<k&&j>0&&i<j;i++,j--){
        			int temp=reverse[i].val;
        			    reverse[i].val=reverse[j].val;
        			    reverse[j].val=temp;
        		} 
        	 }
        	 head=head.next;
        	 index++;
         }
         return pre;
	    }
}
